package com.qfedu.luck.filter;

import com.alibaba.fastjson.JSON;
import com.qfedu.luck.vo.R;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: LuckStudy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-02-24 15:11
 */
@WebFilter("/*")
public class CheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //过滤处理
        Map<String, String[]> map= servletRequest.getParameterMap();
        for(String s:map.keySet()){
            for(String v:map.get(s)){
                if(v==null && v.length()==0){
                    //拦截
                    HttpServletResponse response=(HttpServletResponse)servletResponse;
                    response.setContentType("application/json;charset=UTF-8");
                    response.getWriter().print(JSON.toJSONString(R.fail()));
                    return;
                }
            }
        }
        //放行
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
